package com.mmdccj.yugu.controller;

import com.github.pagehelper.PageInfo;
import com.mmdccj.yugu.common.Result;
import com.mmdccj.yugu.dto.SocialInfoDto;
import com.mmdccj.yugu.service.FollowService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: MMDCCJ
 * @date: 2023-12-09 03:39
 */
@Slf4j
@RestController
@RequestMapping("/follow")
public class FollowController {
    @Autowired
    private FollowService followService;

    /**
     * 获取所有关注的人
     *
     * @param userId      用户id
     * @param currentPage 当前页
     * @return Result<PageInfo < SocialInfoDto>>
     */
    @GetMapping("/follows")
    public Result<PageInfo<SocialInfoDto>> follows(String userId, int currentPage) {
        if (userId == null) {
            return Result.error("缺少参数");
        }
        return Result.success("成功", followService.getAllFollowByUserId(userId, currentPage));
    }

    @GetMapping("/fans")
    public Result<PageInfo<SocialInfoDto>> fans(String userId, int currentPage) {
        if (userId == null) {
            return Result.error("缺少参数");
        }
        return Result.success("成功", followService.getAllFansByUserId(userId, currentPage));
    }

    /**
     * 关注某个用户
     *
     * @param session    session
     * @param followedId 被关注者id
     * @return Result<String> 关注结果
     */
    @PostMapping("/followUser")
    public Result<String> followUser(HttpSession session, String followedId) {
        if (followedId == null) {
            return Result.error("缺少参数");
        }
        String userId = (String) session.getAttribute("user");
        if (userId == null) {
            return Result.error("未登录");
        }
        if (userId.equals(followedId))
            return Result.error("不能关注自己");
        if (followService.followUser(userId, followedId)) {
            return Result.success("成功");
        } else {
            return Result.error("关注失败");
        }
    }

    /**
     * 检查是否关注过某个用户
     *
     * @param session    session
     * @param followedId 被关注者id
     * @return Result<Boolean>
     */
    @GetMapping("/checkFollow")
    public Result<Boolean> checkFollow(HttpSession session, String followedId) {
        if (followedId == null) {
            return Result.error("参数followedId为空");
        }
        String userId = (String) session.getAttribute("user");
        return Result.success("获取成功", followService.checkFollow(userId, followedId));
    }

    /**
     * 取关某人
     *
     * @param session    session
     * @param followedId 被取关者id
     * @return Result<Boolean>
     */
    @PostMapping("/unFollow")
    public Result<Boolean> unFollow(HttpSession session, String followedId) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) {
            return Result.error("未登录");
        }
        if (followService.unFollow(userId, followedId)) {
            return Result.success("取关成功");
        } else {
            return Result.error("取关失败");
        }
    }


}
